from decimal import Decimal
from typing import List

class Snapshot:
    def __init__(self):
        self.__symbol: str = ""
        self.__timestamp: int = 0
        self.__price: Decimal = 0.0
        self.__volume: int = 0
        self.__total_trade_volume: int = 0
        self.__total_trade_value: Decimal = 0.0
        self.__highest_price_today: Decimal = 0.0
        self.__lowest_price_today: Decimal = 0.0
        self.__sell_prices: List[Decimal] = []
        self.__sell_volumes: List[int]  = []
        self.__buy_prices: List[Decimal] = []
        self.__buy_volumes: List[int] = []

    def set_symbol(self, symbol: str):
        self.__symbol = symbol

    def set_timestamp(self, timestamp: int):
        self.__timestamp = timestamp

    def set_price(self, price: Decimal):
        self.__price = price
    
    def set_volume(self, volume: int):
        self.__volume = volume

    def set_total_trade_volume(self, total_trade_volume: int):
        self.__total_trade_volume = total_trade_volume

    def set_total_trade_value(self, total_trade_value: Decimal):
        self.__total_trade_value = total_trade_value

    def set_highest_price_today(self, highest_price_today: Decimal):
        self.__highest_price_today = highest_price_today
    
    def set_lowest_price_today(self, lowest_price_today: Decimal):
        self.__lowest_price_today = lowest_price_today

    def add_sell_price(self, price: Decimal):
        self.__sell_prices.append(price)

    def add_buy_price(self, price: Decimal):
        self.__buy_prices.append(price)

    def add_sell_volume(self, volume: int):
        self.__sell_volumes.append(volume)

    def add_buy_volume(self, volume: int):
        self.__buy_volumes.append(volume)

    def get_symbol(self) -> str:
        return self.__symbol
    
    def get_timestamp(self) -> int:
        return self.__timestamp
    
    def get_price(self) -> Decimal:
        return self.__price
    
    def get_volume(self) -> int:
        return self.__volume
    
    def get_total_trade_volume(self) -> int:
        return self.__total_trade_volume
    
    def get_total_trade_value(self) -> Decimal:
        return self.__total_trade_value
    
    def get_highest_price_today(self) -> Decimal:
        return self.__highest_price_today
    
    def get_lowest_price_today(self) -> Decimal:
        return self.__lowest_price_today
    
    def get_sell_prices(self) -> List[Decimal]:
        return self.__sell_prices
    
    def get_buy_prices(self) -> List[Decimal]:
        return self.__buy_prices
    
    def get_sell_volumes(self) -> List[int]:
        return self.__sell_volumes
    
    def get_buy_volumes(self) -> List[int]:
        return self.__buy_volumes
    
    def to_numpy(self) -> List[float]:
        """将快照转换为 NumPy 数组"""
        return [
            self.__timestamp / 1000.0,  # 转换为秒
            float(self.__price),
            self.__volume,
            self.__total_trade_volume,
            float(self.__total_trade_value),
            float(self.__highest_price_today),
            float(self.__lowest_price_today),
        ] + [float(price) for price in self.__sell_prices] + \
          [float(volume) for volume in self.__sell_volumes] + \
          [float(price) for price in self.__buy_prices] + \
          [float(volume) for volume in self.__buy_volumes]
    
    def __str__(self) -> str:
        lines = []
        lines.append(f"Stock Symbol: {self.__symbol}")
        lines.append(f"Timestamp: {self.__timestamp}")
        lines.append(f"PreClose: {self.__price}")  # 如果有单独昨收价字段可替换
        lines.append(f"High: {self.__highest_price_today}")
        lines.append(f"Low: {self.__lowest_price_today}")
        lines.append(f"Open: 0")   # 你的类里没有开盘价字段，这里填0或以后扩展
        lines.append(f"Close: {self.__price}") 
        lines.append(f"AccTurnover: {self.__total_trade_value}")
        lines.append(f"AccVolume: {self.__total_trade_volume}")

        # 卖盘部分
        lines.append("--- Ask Prices and Volumes ---")
        lines.append("Price\tVolume")
        for i in range(9, -1, -1):  # 卖10 → 卖1
            price = self.__sell_prices[i] if i < len(self.__sell_prices) else Decimal("0")
            vol = self.__sell_volumes[i] if i < len(self.__sell_volumes) else 0
            lines.append(f"{price}\t{vol}")

        # 买盘部分
        lines.append("--- Bid Prices and Volumes ---")
        lines.append("Price\tVolume")
        for i in range(10):
            price = self.__buy_prices[i] if i < len(self.__buy_prices) else Decimal("0")
            vol = self.__buy_volumes[i] if i < len(self.__buy_volumes) else 0
            lines.append(f"{price}\t{vol}")

        return "\n".join(lines)